﻿// JScript 文件

var imaxStars=100;                     //最多星星数
var imax_Y=413;                       //最大Y坐标值
var imax_X=600;                       //最大X坐标值
var maxWait=1;                        //最大等待时间1秒
var startpoint={x:10,y:400};          //星星飞行起点

var istarFrame_x=new Array(50);       //星座框架数组横坐标
var istarFrame_y=new Array(50);       //星座框架数组列坐标

var iusedStars=0;                     //框架中已使用的星星数
var iusedFrameStars=0;                //星座中已使用完的星星数
var icurrStarId=-1;                   //当前选中的浏览者星星
var iwaitTime=0;                      //以等待时间（秒）
var iInterval;                        //系统间隔调用

var imgs=new Array();

function alterFrame()
{
    invokeHandle();
}

/*激活事件控制函数*/
function invokeHandle()
{
     document.getElementById("mainPic").onkeydown=keyDownHandle;
     for(var i=0;i<imaxStars;i++)
     {
        var obj=document.getElementById("star"+(i+1));
        if(obj==null) continue;
        obj.onkeydown=Function("deleteStar("+(i+1)+")");    //删除事件
        obj.onmouseover=null;
        obj.style.visibility="visible";
        Draggable(obj);
        
        obj=document.getElementById("img"+(i+1));
        if(obj.src=="")
            obj.src=imgs[0].src;
     }
     document.getElementById("divLocateUserBless").innerHTML="";
}

/* shift+鼠标左键点击事件处理函数(黑色星空)*/
function addStar()
{   
	var idstar=0;
	//获取空闲的星座
	while(idstar<imaxStars && istarFrame_x[idstar]!=-1)	++idstar;
    if(idstar>=imaxStars)
    {
        alert("星星数目已经达到50个，不可以再添加了");
        return;
    }
	
    var x=event.x;
    var y=event.y;
    if(y>=imax_Y)
        y=413-10;
    if(x>=imax_X)
        x=imax_X-10;
    istarFrame_x[idstar]=x;
    istarFrame_y[idstar]=y;
    idstar++;
    var str="";
    str+="<div id=star"+idstar+" style='position:absolute; Top:"+y+"px;Left:"+x+"px;"
           +"width:14px;height:14px;background-color:black;'"
           +"<input id=info"+idstar+" type=hidden></input>"
           +"<img id=img"+idstar+" src="+imgs[0].src+"/></div>";
    document.getElementById("divStars").innerHTML+=str;
    
    var obj=document.getElementById("star"+idstar);
    obj.onkeydown=Function("deleteStar("+(idstar)+")");    //删除事件   
    obj.style.visibility="visible";
    Draggable(obj);
    document.getElementById("mainPic").onmousedown=null;          
}

function keyDownHandle()
{
    if(event.keyCode==70 || event.kedCode==102)
    {
        document.getElementById("mainPic").onmousedown=addStar;
    }
}

/* 移动星星函数（星星上的）*/
function getStyle(oElm, strCssRule)
{
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        var css = document.defaultView.getComputedStyle(oElm, null);
        strValue = css ? css.getPropertyValue(strCssRule) : null;
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}
    
function Draggable(el)
{
    var xDelta = 0, yDelta = 0;
    var xStart = 0, yStart = 0;
    // remove the events
    function enddrag()
    {
        var obj=document.getElementById("mainPic");
        obj.onmousemove=null;
        el.onmouseup=null;
    }
    // fire each time it's dragged
    function drag(e)
    {
        e = e || window.event;
        xDelta = xStart - parseInt(e.clientX);
        yDelta = yStart - parseInt(e.clientY);
        xStart = parseInt(e.clientX);
        yStart = parseInt(e.clientY);
        var id=el.id;
        id=id.substring(4,id.length);
        var index=parseInt(id)-1;      
        el.style.top = (parseInt(el.style.top) - yDelta) + 'px';
        el.style.left = (parseInt(el.style.left) - xDelta) + 'px';
        istarFrame_y[index]=parseInt(el.style.top);
        istarFrame_x[index]=parseInt(el.style.left);
    }
    // initiate the drag
    function md(e)
    {
        e = e || window.event;
        xStart = parseInt(e.clientX);
        yStart = parseInt(e.clientY);
        el.style.top = parseInt(getStyle(el,'top')) + 'px';
        el.style.left = parseInt(getStyle(el,'left')) + 'px';
        var obj=document.getElementById("mainPic");
        obj.onmousemove=drag;
        el.onmouseup=enddrag;
        return false;
    }
    // tie it into the element
    el.onmousedown = md;
}

function deleteStar(id)
{
	if(id<0 || id>imaxStars)
		return;
	//按下了del键
    if(event.keyCode==46)
    {
        var obj=document.getElementById("img"+id);
       if(obj.src!="" && obj.src!=imgs[0].src)
       {
            alert("此星星已被许愿 ，不可以删除!")
                return;
       }
				
       istarFrame_x[id-1]=-1;
	   istarFrame_y[id-1]=-1;
       
       showStars();
       showStars_User();
       invokeHandle();
    }
}

/* 显示星座的所有星星*/
function showStars()
{
     document.getElementById("mainPic").onkeydown=null;
     var str="";
     for(i=0;i<imaxStars;i++)
     {
		 if(istarFrame_y[i]!=-1)	//为-1表示用户已删除此点坐标
         	str+="<div id=star"+(i+1)+" style='position:absolute; Top:"+ istarFrame_y[i]+"px; Left:"+istarFrame_x[i]+"px;"
            	+"width:14px;height:14px;background-color:black; visibility:hidden; '>"
           		+"<input id=info"+(i+1)+" type=hidden ></input>"
            	+"<img id=img"+(i+1)+"  width=14px height=14px/></div>";
     }
     document.getElementById("divStars").innerHTML=str;
}

/* 预载入图片和初始化*/
function preload()
{
     imgs[0]=new Image();
     imgs[0].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/s1.JPG";
     //imgs[1]=new Image();
     //imgs[1].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/1-2.jpg";
     imgs[2]=new Image();
     imgs[2].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/11.gif";
     imgs[3]=new Image();
     imgs[3].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/P1020686.JPG";
     imgs[4]=new Image();
     imgs[4].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/jupiter.gif";
     imgs[5]=new Image();
     imgs[5].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/mars.gif";
     imgs[6]=new Image();
     imgs[6].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/saturn.gif";
     imgs[7]=new Image();
     imgs[7].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/uranus.gif";
      imgs[8]=new Image();
     imgs[8].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/st1.gif";
      imgs[9]=new Image();
     imgs[9].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/st2.gif";
      imgs[10]=new Image();
     imgs[10].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/st3.gif";
      imgs[11]=new Image();
     imgs[11].src="http://my-first-test.googlecode.com/svn/trunk/vs2005/appstar/img/st4.gif";
     document.getElementById("t1").background=imgs[2].src;
     document.getElementById("mainPic").background=imgs[3].src;
}

/* 星星上天空飞行函数*/
function starFly(id)
{
    var obj=document.getElementById("star"+id);
    var ey=parseInt(obj.style.top);
    var ex=parseInt(obj.style.left);
    var k=(-200)*1.0/((-200)*(-200));
    window.setTimeout('ymove('+ex+','+ey+','+k+','+id+','+50+')',500); 
}

 function ymove(x,y,ek,id,times)
 {  
   var count=parseInt(times);
   var ex=parseInt(x);
   var ey=parseInt(y);
   var k=parseFloat(ek);
   count--;
   var s=x-count*4;
   var obj=document.getElementById("star"+id);
   if(count==0)
   {
        obj.style.left=ex;
        obj.style.top=ey;
        return;
   }
   obj.style.left=s;
   s=Math.round(k*(s-x)*(s-x)-y);           //计算弧线y轴坐标
   obj.style.top=-s;
   window.setTimeout('ymove('+x+','+y+','+ek+','+id+','+count+')',60);   
}

/* 用户许下自己的心愿，插入到一个新的星星中*/
function addStarValid()
{
    var text="";
    var s;
    s=document.getElementById("starname").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星名称");
        document.getElementById("starname").focus();
        return;
    }
    text+=s+"|";
    var radio=document.getElementsByName("starType");
    if(radio!=null)
    {
        for(var i=0; i<radio.length; i++)
            if(radio[i].checked)
            {
                s=trim(radio[i].value);
                text+=s+"|";
                break;
            }
    }
    s=document.getElementById("starspec").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星简要说明");
        document.getElementById("starspec").focus();
        return;
    }
    text+=s+"|";
    s=document.getElementById("starbless").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星祝福语");
        document.getElementById("starbless").focus();
        return;
    }
    text+=s;
    saveAddStar(text);
}

/* 用户修改自己的心愿*/
function userAlterStarValid()
{
    var text="";
    var s;
    s=document.getElementById("starname_a").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星名称");
        document.getElementById("starname_a").focus();
        return;
    }
    text+=s+"|";
    var radio=document.getElementsByName("starType_a");
    if(radio!=null)
    {
        for(var i=0; i<radio.length; i++)
            if(radio[i].checked)
            {
                s=trim(radio[i].value);
                text+=s+"|";
                break;
            }
    }
    s=document.getElementById("starspec_a").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星简要说明");
        document.getElementById("starspec_a").focus();
        return;
    }
    text+=s+"|";
    s=document.getElementById("starbless_a").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星祝福语");
        document.getElementById("starbless_a").focus();
        return;
    }
    text+=s;
    alterSelfBless(text);
}

/*删除字符串左右两端的空格*/
function trim(str)
{ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

/*显示星星的详细说明div层*/
function $(element)
{
    return element = document.getElementById(element);
}

function userStarOnmouseover(id)
{
    iwaitTime++;
    if(iwaitTime>maxWait)
    {
        var star=$("star"+id);
        var info=$("info"+id).value;
        var obj=$("showInfo");
        obj.style.top=parseInt(star.style.top)+5+'px';
        obj.style.left=parseInt(star.style.left)+5+'px';
        
        document.getElementById("mainPic").onmouseover=userStarOnmouseout;
        
        var s=info.split('|');
        document.getElementById("starName_table").innerText=s[2];
        var t=parseInt(s[3]);
        document.getElementById("mystarType").src=imgs[t+3].src;
        var st=document.getElementById("starType_table");
        if(t==1)    st.innerText="木星";
        else if(t==2)    st.innerText="天王星";
        else if(t==3)    st.innerText="土星";
        else if(t==4)    st.innerText="火星";
        //document.getElementById("starType_table").innerText=s[3];
        
        var friend;
        if(s[6]==owner.getId())
            friend=owner;
        else
            friend=ownerFriends.getById(s[6]);
        document.getElementById("starFounder_table").innerText=friend.getDisplayName();
        document.getElementById("starSepc_table").innerText=s[4];
        document.getElementById("starBless_table").innerText=s[5];
        
        obj.style.display='block';
    }
    else
        iInterval=window.setTimeout("userStarOnmouseover("+id+")",500); 
}

function userStarOnmouseout()
{
    window.clearTimeout(iInterval);
    if(iwaitTime>maxWait)
    {
        var obj=$("showInfo");
        obj.style.display='none';
        document.getElementById("mainPic").onmouseover=null;
    }
    iwaitTime=0;
}

/*地位某个用户的祝福星星*/
function locateUerBless(uid)
{
    var res=document.getElementsByTagName("input");
    var str="";
    var obj;
    if(res!=null)
    {
       // alert(res.length);
        for(var i=0; i<res.length; i++)
        {
            obj=res[i];
            var id=obj.id;
            if(id.substring(0,4)=="info")
            {
                id=id.substring(4,id.length);
                var s=obj.value.split('|');
                obj=document.getElementById("star"+id);
                //alert(id);
                if(obj!=null && s[6]==uid)
                {
                    var top=parseInt(obj.style.top);
                    var left=parseInt(obj.style.left);
                    var width=parseInt(obj.style.width);
                    var height=parseInt(obj.style.width);
                    str+="<DIV id=locateUser style=\"position:absolute; top:"+(top-30)+"; left:"+(left-30)
                       +"; BORDER-RIGHT: yellow 3px double; BORDER-TOP: yellow 3px double"
                       +" BACKGROUND: transprant; BORDER-LEFT: yellow 3px double; WIDTH:"+(width+60)
                       +"; BORDER-BOTTOM: yellow 3px double; HEIGHT:"+(height+60)+";\" align=left></DIV>";
                }
            }
         }
     }
    obj=document.getElementById("divLocateUserBless");
    if(obj!=null)
        obj.innerHTML=str;
    changeShapeLocate();
}

function changeShapeLocate()
{
    var obj=document.getElementsByName("locateUser");
    for(var i=0; i<obj.length;i++)
    {
        var t=obj[i];
        var s=parseInt(t.style.width);
        if(s<=26)
        {
            return;
        }
        t.style.width=s-2;
        t.style.height=s-2;
        t.style.top=parseInt(t.style.top)+1;
        t.style.left=parseInt(t.style.left)+1;
    }
    window.setTimeout(changeShapeLocate,80);
}

/*只显示当前浏览者的星星*/
function showViewerBless()
{
    icurrStarId=-1;
    document.getElementById("starname_a").value="";
    document.getElementById("starspec_a").value="";
    document.getElementById("starbless_a").value="";
    
    var vid=viewer.getId();
    var res=document.getElementsByTagName("input");
    var str="";
    if(res!=null)
    {
        for(var i=0; i<res.length; i++)
        {
            var obj=res[i];
            var id=obj.id;
            if(id.substring(0,4)=="info")
            {
                id=id.substring(4,id.length);
                var s=obj.value.split('|');
                if(obj!=null && s[6]==vid)
                {
                     var j=parseInt(id);
                     str+="<div id=star"+j+" style='position:absolute; Top:"+ istarFrame_y[j-1]+"px; Left:"+istarFrame_x[j-1]+"px;"
                        +"width:14px;height:14px;background-color:#FFEFFE; visibility:visible;' onclick=showViewerInfo("+j+") >"
                        +"<input id=info"+j+" type=hidden value="+obj.value+"></input>"
                        +"<img id=img"+j+" src="+imgs[parseInt(s[3])+7].src+" /></div>";
                }
            }
        }
      document.getElementById("divStars").innerHTML=str;
    }
 }
         
 /*只显示当前浏览者的星星信息*/
 function showViewerInfo(id)
 {
    icurrStarId=id;
    var obj=document.getElementById("info"+id);
    if(obj!=null)
    {
        var s=obj.value.split('|');
        for(var i=0;i<s.length;i++)
        {
            document.getElementById("starname_a").value=s[2];
            var radio=document.getElementsByName("starType_a");
            for(var j=0;j<radio.length;j++)
            {
                 if(radio[j].value==s[3])
                {
                    radio[j].checked='CHECKED';
                    break;
                }
            }
            document.getElementById("starspec_a").value=s[4];
            document.getElementById("starbless_a").value=s[5];
        }
    }
 }
 
 function setStatus(obj,text)
{
    if(obj!=null)
    {
        obj.innerText=text;
    }
}

    